home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 4.iso / public / jpeg / libCLRjpeg4 / src / jpeglib.c next >
C/C++ Source or Header  |  1994-08-01  |  31KB  |  796 lines

  1. /*
  2.  * example.c
  3.  *
  4.  * This file is not actually part of the JPEG software.  Rather, it provides
  5.  * a skeleton that may be useful for constructing applications that use the
  6.  * JPEG software as subroutines.  This code will NOT do anything useful as is.
  7.  *
  8.  * This file illustrates how to use the JPEG code as a subroutine library
  9.  * to read or write JPEG image files.  We assume here that you are not
  10.  * merely interested in converting the image to yet another image file format
  11.  * (if you are, you should be adding another I/O module to cjpeg/djpeg, not
  12.  * constructing a new application).  Instead, we show how to pass the
  13.  * decompressed image data into or out of routines that you provide.  For
  14.  * example, a viewer program might use the JPEG decompressor together with
  15.  * routines that write the decompressed image directly to a display.
  16.  *
  17.  * We present these routines in the same coding style used in the JPEG code
  18.  * (ANSI function definitions, etc); but you are of course free to code your
  19.  * routines in a different style if you prefer.
  20.  */
  21.  
  22. /*
  23.  * Include file for declaring JPEG data structures.
  24.  * This file also includes some system headers like <stdio.h>;
  25.  * if you prefer, you can include "jconfig.h" and "jpegdata.h" instead.
  26.  */
  27.  
  28. #include "jinclude.h"
  29. #include "jversion.h"
  30.  
  31. /*
  32.  * <setjmp.h> is used for the optional error recovery mechanism shown in
  33.  * the second part of the example.
  34.  */
  35.  
  36. #include <setjmp.h>
  37.  
  38. #include <gl.h>
  39. #include "CLRjpegType.h"
  40. static unsigned long *jpegimgptr;
  41. int StartX,StartY,EndX,EndY;
  42. char ColourType;
  43. static int trow=0;
  44. static int crow=0;
  45. static char readtype;
  46. static int readxsize,readysize;
  47. static int readstartx, readstarty;
  48.  
  49.  
  50. /******************** JPEG COMPRESSION SAMPLE INTERFACE *******************/
  51.  
  52. /* This half of the example shows how to feed data into the JPEG compressor.
  53.  * We present a minimal version that does not worry about refinements such
  54.  * as error recovery (the JPEG code will just exit() if it gets an error).
  55.  */
  56.  
  57.  
  58. /*
  59.  * To supply the image data for compression, you must define three routines
  60.  * input_init, get_input_row, and input_term.  These routines will be called
  61.  * from the JPEG compressor via function pointer values that you store in the
  62.  * cinfo data structure; hence they need not be globally visible and the exact
  63.  * names don't matter.  (In fact, the "METHODDEF" macro expands to "static" if
  64.  * you use the unmodified JPEG include files.)
  65.  *
  66.  * The input file reading modules (jrdppm.c, jrdgif.c, jrdtarga.c, etc) may be
  67.  * useful examples of what these routines should actually do, although each of
  68.  * them is encrusted with a lot of specialized code for its own file format.
  69.  */
  70.  
  71.  
  72. METHODDEF void
  73. input_init (compress_info_ptr cinfo)
  74. /* Initialize for input; return image size and component data. */
  75. {
  76.   /* This routine must return five pieces of information about the incoming
  77.    * image, and must do any setup needed for the get_input_row routine.
  78.    * The image information is returned in fields of the cinfo struct.
  79.    * (If you don't care about modularity, you could initialize these fields
  80.    * in the main JPEG calling routine, and make this routine be a no-op.)
  81.    * We show some example values here.
  82.    */
  83.   cinfo->image_width = EndX-StartX+1;        /* width in pixels */
  84.   cinfo->image_height = EndY-StartY+1;        /* height in pixels */
  85.   /* JPEG views an image as being a rectangular array of pixels, with each
  86.    * pixel having the same number of "component" values (color channels).
  87.    * You must specify how many components there are and the colorspace
  88.    * interpretation of the components.  Most applications will use RGB data or
  89.    * grayscale data.  If you want to use something else, you'll need to study
  90.    * and perhaps modify jcdeflts.c, jccolor.c, and jdcolor.c.
  91.    */
  92.     if(ColourType == 0) {
  93.   cinfo->input_components = 1;        /* or 1 for grayscale */
  94.   cinfo->in_color_space = CS_GRAYSCALE;    /* or CS_GRAYSCALE for grayscale */
  95.     } else {
  96.   cinfo->input_components = 3;        /* or 1 for grayscale */
  97.   cinfo->in_color_space = CS_RGB;    /* or CS_GRAYSCALE for grayscale */
  98.     }
  99.   cinfo->data_precision = 8;        /* bits per pixel component value */
  100.   /* In the current JPEG software, data_precision must be set equal to
  101.    * BITS_IN_JSAMPLE, which is 8 unless you twiddle jconfig.h.  Future
  102.    * versions might allow you to say either 8 or 12 if compiled with
  103.    * 12-bit JSAMPLEs, or up to 16 in lossless mode.  In any case,
  104.    * it is up to you to scale incoming pixel values to the range
  105.    *   0 .. (1<<data_precision)-1.
  106.    * If your image data format is fixed at a byte per component,
  107.    * then saying "8" is probably the best long-term solution.
  108.    */
  109. }
  110.  
  111.  
  112. /*
  113.  * This function is called repeatedly and must supply the next row of pixels
  114.  * on each call.  The rows MUST be returned in top-to-bottom order if you want
  115.  * your JPEG files to be compatible with everyone else's.  (If you cannot
  116.  * readily read your data in that order, you'll need an intermediate array to
  117.  * hold the image.  See jrdtarga.c or jrdrle.c for examples of handling
  118.  * bottom-to-top source data using the JPEG code's portable mechanisms.)
  119.  * The data is to be returned into a 2-D array of JSAMPLEs, indexed as
  120.  *        JSAMPLE pixel_row[component][column]
  121.  * where component runs from 0 to cinfo->input_components-1, and column runs
  122.  * from 0 to cinfo->image_width-1 (column 0 is left edge of image).  Note that
  123.  * this is actually an array of pointers to arrays rather than a true 2D array,
  124.  * since C does not support variable-size multidimensional arrays.
  125.  * JSAMPLE is typically typedef'd as "unsigned char".
  126.  */
  127.  
  128.  
  129. METHODDEF void
  130. PTRget_input_row (compress_info_ptr cinfo, JSAMPARRAY pixel_row)
  131. /* Read next row of pixels into pixel_row[][] */
  132. {
  133.   /* This example shows how you might read RGB data (3 components)
  134.    * from an input file in which the data is stored 3 bytes per pixel
  135.    * in left-to-right, top-to-bottom order.
  136.    */
  137.   register FILE * infile = cinfo->input_file;
  138.   register JSAMPROW ptr0, ptr1, ptr2;
  139.   register long col;
  140.     unsigned long *rpos;
  141.     unsigned char rb[1280],gb[1280],bb[1280];
  142.   
  143.     crow++;
  144.     rpos = jpegimgptr + (cinfo->image_width * (cinfo->image_height - crow));
  145.   ptr0 = pixel_row[0];
  146.   ptr1 = pixel_row[1];
  147.   ptr2 = pixel_row[2];
  148.   for (col = 0; col < cinfo->image_width; col++) {
  149.     *ptr0++ = (char)(*rpos & 0xff);
  150.     *ptr1++ = (char)((*rpos & 0xff00)>>8);
  151.     *ptr2++ = (char)((*rpos & 0xff0000)>>16);
  152.     rpos++;
  153.   }
  154. }
  155.  
  156. METHODDEF void
  157. SCREENget_input_row (compress_info_ptr cinfo, JSAMPARRAY pixel_row)
  158. /* Read next row of pixels into pixel_row[][] */
  159. {
  160.   /* This example shows how you might read RGB data (3 components)
  161.    * from an input file in which the data is stored 3 bytes per pixel
  162.    * in left-to-right, top-to-bottom order.
  163.    */
  164.     static int crow=0;
  165.   register FILE * infile = cinfo->input_file;
  166.   register JSAMPROW ptr0, ptr1, ptr2;
  167.   register long col;
  168.     unsigned long row[1280];
  169.     unsigned long *rpos;
  170.     unsigned char rb[1280],gb[1280],bb[1280];
  171.  
  172. /*
  173.     lrectread(StartX,EndY-crow,EndX,EndY-crow,row);
  174. */
  175.     cmov2i(StartX,EndY-crow);
  176. #if 0
  177.     readRGB(cinfo->image_width,rb,gb,bb);
  178. #endif
  179.     gl_readscreen(cinfo->image_width,rb,gb,bb);
  180.     ++crow;
  181.     rpos = row;
  182.   ptr0 = pixel_row[0];
  183.   ptr1 = pixel_row[1];
  184.   ptr2 = pixel_row[2];
  185.   for (col = 0; col < cinfo->image_width; col++) {
  186.     *ptr0++ = (char)rb[col];
  187.     *ptr1++ = (char)gb[col];
  188.     *ptr2++ = (char)bb[col];
  189.   }
  190. }
  191.  
  192. METHODDEF void
  193. input_term (compress_info_ptr cinfo)
  194. /* Finish up at the end of the input */
  195. {
  196.   /* This termination routine will very often have no work to do, */
  197.   /* but you must provide it anyway. */
  198.   /* Note that the JPEG code will only call it during successful exit; */
  199.   /* if you want it called during error exit, you gotta do that yourself. */
  200. }
  201.  
  202.  
  203. /*
  204.  * That's it for the routines that deal with reading the input image data.
  205.  * Now we have overall control and parameter selection routines.
  206.  */
  207.  
  208.  
  209. /*
  210.  * This routine must determine what output JPEG file format is to be written,
  211.  * and make any other compression parameter changes that are desirable.
  212.  * This routine gets control after the input file header has been read
  213.  * (i.e., right after input_init has been called).  You could combine its
  214.  * functions into input_init, or even into the main control routine, but
  215.  * if you have several different input_init routines, it's a definite win
  216.  * to keep this separate.  You MUST supply this routine even if it's a no-op.
  217.  */
  218.  
  219. METHODDEF void
  220. c_ui_method_selection (compress_info_ptr cinfo)
  221. {
  222.   /* If the input is gray scale, generate a monochrome JPEG file. */
  223.   if (cinfo->in_color_space == CS_GRAYSCALE)
  224.     j_monochrome_default(cinfo);
  225.   /* For now, always select JFIF output format. */
  226.   jselwjfif(cinfo);
  227. }
  228.  
  229.  
  230. /*
  231.  * OK, here is the main function that actually causes everything to happen.
  232.  * We assume here that the target filename is supplied by the caller of this
  233.  * routine, and that all JPEG compression parameters can be default values.
  234.  */
  235.  
  236. GLOBAL void
  237. write_JPEG_file (char * filename,char typefrom)
  238. {
  239.   /* These three structs contain JPEG parameters and working data.
  240.    * They must survive for the duration of parameter setup and one
  241.    * call to jpeg_compress; typically, making them local data in the
  242.    * calling routine is the best strategy.
  243.    */
  244.   struct Compress_info_struct cinfo;
  245.   struct Compress_methods_struct c_methods;
  246.   struct External_methods_struct e_methods;
  247.  
  248.   /* Initialize the system-dependent method pointers. */
  249.   cinfo.methods = &c_methods;    /* links to method structs */
  250.   cinfo.emethods = &e_methods;
  251.   /* Here we use the default JPEG error handler, which will just print
  252.    * an error message on stderr and call exit().  See the second half of
  253.    * this file for an example of more graceful error recovery.
  254.    */
  255.   jselerror(&e_methods);    /* select std error/trace message routines */
  256.   /* Here we use the standard memory manager provided with the JPEG code.
  257.    * In some cases you might want to replace the memory manager, or at
  258.    * least the system-dependent part of it, with your own code.
  259.    */
  260.   jselmemmgr(&e_methods);    /* select std memory allocation routines */
  261.   /* If the compressor requires full-image buffers (for entropy-coding
  262.    * optimization or a noninterleaved JPEG file), it will create temporary
  263.    * files for anything that doesn't fit within the maximum-memory setting.
  264.    * (Note that temp files are NOT needed if you use the default parameters.)
  265.    * You can change the default maximum-memory setting by changing
  266.    * e_methods.max_memory_to_use after jselmemmgr returns.
  267.    * On some systems you may also need to set up a signal handler to
  268.    * ensure that temporary files are deleted if the program is interrupted.
  269.    * (This is most important if you are on MS-DOS and use the jmemdos.c
  270.    * memory manager back end; it will try to grab extended memory for
  271.    * temp files, and that space will NOT be freed automatically.)
  272.    * See jcmain.c or jdmain.c for an example signal handler.
  273.    */
  274.  
  275.   /* Here, set up pointers to your own routines for input data handling
  276.    * and post-init parameter selection.
  277.    */
  278.   c_methods.input_init = input_init;
  279.     switch(typefrom) {
  280.         case JPEG_SCREEN:
  281.             c_methods.get_input_row = SCREENget_input_row;
  282.             break;
  283.         case JPEG_PTR:
  284.             c_methods.get_input_row = PTRget_input_row;
  285.             break;
  286.     }
  287.   c_methods.input_term = input_term;
  288.   c_methods.c_ui_method_selection = c_ui_method_selection;
  289.  
  290.   /* Set up default JPEG parameters in the cinfo data structure. */
  291.   j_c_defaults(&cinfo, 75, FALSE);
  292.   /* Note: 75 is the recommended default quality level; you may instead pass
  293.    * a user-specified quality level.  Be aware that values below 25 will cause
  294.    * non-baseline JPEG files to be created (and a warning message to that
  295.    * effect to be emitted on stderr).  This won't bother our decoder, but some
  296.    * commercial JPEG implementations may choke on non-baseline JPEG files.
  297.    * If you want to force baseline compatibility, pass TRUE instead of FALSE.
  298.    * (If non-baseline files are fine, but you could do without that warning
  299.    * message, set e_methods.trace_level to -1.)
  300.    */
  301.  
  302.   /* At this point you can modify the default parameters set by j_c_defaults
  303.    * as needed.  For a minimal implementation, you shouldn't need to change
  304.    * anything.  See jcmain.c for some examples of what you might change.
  305.    */
  306.  
  307.   /* Select the input and output files.
  308.    * Note that cinfo.input_file is only used if your input reading routines
  309.    * use it; otherwise, you can just make it NULL.
  310.    * VERY IMPORTANT: use "b" option to fopen() if you are on a machine that
  311.    * requires it in order to write binary files.
  312.    */
  313.  
  314.   cinfo.input_file = NULL;    /* if no actual input file involved */
  315.  
  316.   if ((cinfo.output_file = fopen(filename, "wb")) == NULL) {
  317.     fprintf(stderr, "can't open %s\n", filename);
  318.     exit(1);
  319.   }
  320.  
  321.   /* Here we go! */
  322.   jpeg_compress(&cinfo);
  323.  
  324.   /* That's it, son.  Nothin' else to do, except close files. */
  325.   /* Here we assume only the output file need be closed. */
  326.   fclose(cinfo.output_file);
  327.  
  328.   /* Note: if you want to compress more than one image, we recommend you
  329.    * repeat this whole routine.  You MUST repeat the j_c_defaults()/alter
  330.    * parameters/jpeg_compress() sequence, as some data structures allocated
  331.    * in j_c_defaults are freed upon exit from jpeg_compress.
  332.    */
  333. }
  334.  
  335. SaveJPEGImagePtr(char *name,unsigned long *ptr,int xsize, int ysize) {
  336.     crow = 0;
  337.     trow = 0;
  338.     StartX=0;
  339.     EndX=xsize-1;
  340.     StartY=0;
  341.     EndY=ysize-1;
  342.     ColourType = CS_RGB;
  343.     jpegimgptr = ptr;
  344.     write_JPEG_file(name,JPEG_PTR);
  345. }
  346.  
  347. SaveJPEGImageScreen(char *name,int x1,int y1,int x2,int y2) {
  348.     crow = 0;
  349.     trow = 0;
  350.     StartX=x1;
  351.     EndX=x2;
  352.     StartY=y1;
  353.     EndY=y2;
  354.     ColourType = CS_RGB;
  355.     write_JPEG_file(name,JPEG_SCREEN);
  356. }
  357.  
  358. /******************** JPEG DECOMPRESSION SAMPLE INTERFACE *******************/
  359.  
  360. /* This half of the example shows how to read data from the JPEG decompressor.
  361.  * It's a little more refined than the above in that we show how to do your
  362.  * own error recovery.  If you don't care about that, you don't need these
  363.  * next two routines.
  364.  */
  365.  
  366.  
  367. /*
  368.  * These routines replace the default trace/error routines included with the
  369.  * JPEG code.  The example trace_message routine shown here is actually the
  370.  * same as the standard one, but you could modify it if you don't want messages
  371.  * sent to stderr.  The example error_exit routine is set up to return
  372.  * control to read_JPEG_file() rather than calling exit().  You can use the
  373.  * same routines for both compression and decompression error recovery.
  374.  */
  375.  
  376. /* These static variables are needed by the error routines. */
  377. static jmp_buf setjmp_buffer;    /* for return to caller */
  378. static external_methods_ptr emethods; /* needed for access to message_parm */
  379.  
  380.  
  381. /* This routine is used for any and all trace, debug, or error printouts
  382.  * from the JPEG code.  The parameter is a printf format string; up to 8
  383.  * integer data values for the format string have been stored in the
  384.  * message_parm[] field of the external_methods struct.
  385.  */
  386.  
  387. METHODDEF void
  388. trace_message (const char *msgtext)
  389. {
  390.   fprintf(stderr, msgtext,
  391.       emethods->message_parm[0], emethods->message_parm[1],
  392.       emethods->message_parm[2], emethods->message_parm[3],
  393.       emethods->message_parm[4], emethods->message_parm[5],
  394.       emethods->message_parm[6], emethods->message_parm[7]);
  395.   fprintf(stderr, "\n");    /* there is no \n in the format string! */
  396. }
  397.  
  398. /*
  399.  * The error_exit() routine should not return to its caller.  The default
  400.  * routine calls exit(), but here we assume that we want to return to
  401.  * read_JPEG_file, which has set up a setjmp context for the purpose.
  402.  * You should make sure that the free_all method is called, either within
  403.  * error_exit or after the return to the outer-level routine.
  404.  */
  405.  
  406. METHODDEF void
  407. error_exit (const char *msgtext)
  408. {
  409.   trace_message(msgtext);    /* report the error message */
  410.   (*emethods->free_all) ();    /* clean up memory allocation & temp files */
  411.   longjmp(setjmp_buffer, 1);    /* return control to outer routine */
  412. }
  413.  
  414.  
  415.  
  416. /*
  417.  * To accept the image data from decompression, you must define four routines
  418.  * output_init, put_color_map, put_pixel_rows, and output_term.
  419.  *
  420.  * You must understand the distinction between full color output mode
  421.  * (N independent color components) and colormapped output mode (a single
  422.  * output component representing an index into a color map).  You should use
  423.  * colormapped mode to write to a colormapped display screen or output file.
  424.  * Colormapped mode is also useful for reducing grayscale output to a small
  425.  * number of gray levels: when using the 1-pass quantizer on grayscale data,
  426.  * the colormap entries will be evenly spaced from 0 to MAX_JSAMPLE, so you
  427.  * can regard the indexes are directly representing gray levels at reduced
  428.  * precision.  In any other case, you should not depend on the colormap
  429.  * entries having any particular order.
  430.  * To get colormapped output, set cinfo->quantize_colors to TRUE and set
  431.  * cinfo->desired_number_of_colors to the maximum number of entries in the
  432.  * colormap.  This can be done either in your main routine or in
  433.  * d_ui_method_selection.  For grayscale quantization, also set
  434.  * cinfo->two_pass_quantize to FALSE to ensure the 1-pass quantizer is used
  435.  * (presently this is the default, but it may not be so in the future).
  436.  *
  437.  * The output file writing modules (jwrppm.c, jwrgif.c, jwrtarga.c, etc) may be
  438.  * useful examples of what these routines should actually do, although each of
  439.  * them is encrusted with a lot of specialized code for its own file format.
  440.  */
  441.  
  442.  
  443. METHODDEF void
  444. output_init (decompress_info_ptr cinfo)
  445. /* This routine should do any setup required */
  446. {
  447.   /* This routine can initialize for output based on the data passed in cinfo.
  448.    * Useful fields include:
  449.    *    image_width, image_height    Pretty obvious, I hope.
  450.    *    data_precision            bits per pixel value; typically 8.
  451.    *    out_color_space            output colorspace previously requested
  452.    *    color_out_comps            number of color components in same
  453.    *    final_out_comps            number of components actually output
  454.    * final_out_comps is 1 if quantize_colors is true, else it is equal to
  455.    * color_out_comps.
  456.    *
  457.    * If you have requested color quantization, the colormap is NOT yet set.
  458.    * You may wish to defer output initialization until put_color_map is called.
  459.    */
  460. }
  461.  
  462.  
  463. /*
  464.  * This routine is called if and only if you have set cinfo->quantize_colors
  465.  * to TRUE.  It is given the selected colormap and can complete any required
  466.  * initialization.  This call will occur after output_init and before any
  467.  * calls to put_pixel_rows.  Note that the colormap pointer is also placed
  468.  * in a cinfo field, whence it can be used by put_pixel_rows or output_term.
  469.  * num_colors will be less than or equal to desired_number_of_colors.
  470.  *
  471.  * The colormap data is supplied as a 2-D array of JSAMPLEs, indexed as
  472.  *        JSAMPLE colormap[component][indexvalue]
  473.  * where component runs from 0 to cinfo->color_out_comps-1, and indexvalue
  474.  * runs from 0 to num_colors-1.  Note that this is actually an array of
  475.  * pointers to arrays rather than a true 2D array, since C does not support
  476.  * variable-size multidimensional arrays.
  477.  * JSAMPLE is typically typedef'd as "unsigned char".  If you want your code
  478.  * to be as portable as the JPEG code proper, you should always access JSAMPLE
  479.  * values with the GETJSAMPLE() macro, which will do the right thing if the
  480.  * machine has only signed chars.
  481.  */
  482.  
  483. METHODDEF void
  484. put_color_map (decompress_info_ptr cinfo, int num_colors, JSAMPARRAY colormap)
  485. /* Write the color map */
  486. {
  487.   /* You need not provide this routine if you always set cinfo->quantize_colors
  488.    * FALSE; but a safer practice is to provide it and have it just print an
  489.    * error message, like this:
  490.    */
  491.   fprintf(stderr, "put_color_map called: there's a bug here somewhere!\n");
  492. }
  493.  
  494.  
  495. /*
  496.  * This function is called repeatedly, with a few more rows of pixels supplied
  497.  * on each call.  With the current JPEG code, some multiple of 8 rows will be
  498.  * passed on each call except the last, but it is extremely bad form to depend
  499.  * on this.  You CAN assume num_rows > 0.
  500.  * The data is supplied in top-to-bottom row order (the standard order within
  501.  * a JPEG file).  If you cannot readily use the data in that order, you'll
  502.  * need an intermediate array to hold the image.  See jwrrle.c for an example
  503.  * of outputting data in bottom-to-top order.
  504.  *
  505.  * The data is supplied as a 3-D array of JSAMPLEs, indexed as
  506.  *        JSAMPLE pixel_data[component][row][column]
  507.  * where component runs from 0 to cinfo->final_out_comps-1, row runs from 0 to
  508.  * num_rows-1, and column runs from 0 to cinfo->image_width-1 (column 0 is
  509.  * left edge of image).  Note that this is actually an array of pointers to
  510.  * pointers to arrays rather than a true 3D array, since C does not support
  511.  * variable-size multidimensional arrays.
  512.  * JSAMPLE is typically typedef'd as "unsigned char".  If you want your code
  513.  * to be as portable as the JPEG code proper, you should always access JSAMPLE
  514.  * values with the GETJSAMPLE() macro, which will do the right thing if the
  515.  * machine has only signed chars.
  516.  *
  517.  * If quantize_colors is true, then there is only one component, and its values
  518.  * are indexes into the previously supplied colormap.  Otherwise the values
  519.  * are actual data in your selected output colorspace.
  520.  */
  521.  
  522.  
  523. METHODDEF void
  524. SCREENput_pixel_rows (decompress_info_ptr cinfo, int num_rows, JSAMPIMAGE pixel_data)
  525. /* Write some rows of output data */
  526. {
  527.   /* This example shows how you might write full-color RGB data (3 components)
  528.    * to an output file in which the data is stored 3 bytes per pixel.
  529.    */
  530.   register FILE * outfile = cinfo->output_file;
  531.   register JSAMPROW ptr0, ptr1, ptr2;
  532.   register long col;
  533.   register int row;
  534.     char r,g,b;
  535.   
  536.   for (row = 0; row < num_rows; row++) {
  537.     ptr0 = pixel_data[0][row];
  538.     ptr1 = pixel_data[1][row];
  539.     ptr2 = pixel_data[2][row];
  540.     for (col = 0; col < cinfo->image_width; col++) {
  541.         r = GETJSAMPLE(*ptr0);        
  542.         r = r*(8/cinfo->data_precision);
  543.         ptr0++;
  544.         g = GETJSAMPLE(*ptr1);        
  545.         g = g*(8/cinfo->data_precision);
  546.         ptr1++;
  547.         b = GETJSAMPLE(*ptr2);        
  548.         b = b*(8/cinfo->data_precision);
  549.         ptr2++;
  550.         RGBcolor(r,g,b);
  551.         pnt2i(readstartx+col,readstarty+cinfo->image_height-trow);
  552.     }
  553.     ++trow;
  554.   }
  555. }
  556.  
  557. METHODDEF void
  558. PTRput_pixel_rows (decompress_info_ptr cinfo, int num_rows, JSAMPIMAGE pixel_data)
  559. /* Write some rows of output data */
  560. {
  561.   /* This example shows how you might write full-color RGB data (3 components)
  562.    * to an output file in which the data is stored 3 bytes per pixel.
  563.    */
  564.   register FILE * outfile = cinfo->output_file;
  565.   register JSAMPROW ptr0, ptr1, ptr2;
  566.   register long col;
  567.   register int row;
  568.     long *rpos;
  569.     char r,g,b;
  570.   
  571.   for (row = 0; row < num_rows; row++) {
  572.     ptr0 = pixel_data[0][row];
  573.     ptr1 = pixel_data[1][row];
  574.     ptr2 = pixel_data[2][row];
  575.     for (col = 0; col < cinfo->image_width; col++) {
  576.         r = GETJSAMPLE(*ptr0);        
  577.         r = r*(8/cinfo->data_precision);
  578.         ptr0++;
  579.         g = GETJSAMPLE(*ptr1);        
  580.         g = g*(8/cinfo->data_precision);
  581.         ptr1++;
  582.         b = GETJSAMPLE(*ptr2);        
  583.         b = b*(8/cinfo->data_precision);
  584.         ptr2++;
  585.         rpos = jpegimgptr + ((cinfo->image_height-trow)*cinfo->image_width - (cinfo->image_width-col));
  586.         *rpos = 0xff000000 | (b<<16) | (g<<8) | r;
  587.     }
  588.     ++trow;
  589.   }
  590. }
  591.  
  592. METHODDEF void
  593. output_term (decompress_info_ptr cinfo)
  594. /* Finish up at the end of the output */
  595. {
  596.   /* This termination routine may not need to do anything. */
  597.   /* Note that the JPEG code will only call it during successful exit; */
  598.   /* if you want it called during error exit, you gotta do that yourself. */
  599. }
  600.  
  601.  
  602. /*
  603.  * That's it for the routines that deal with writing the output image.
  604.  * Now we have overall control and parameter selection routines.
  605.  */
  606.  
  607.  
  608. /*
  609.  * This routine gets control after the JPEG file header has been read;
  610.  * at this point the image size and colorspace are known.
  611.  * The routine must determine what output routines are to be used, and make
  612.  * any decompression parameter changes that are desirable.  For example,
  613.  * if it is found that the JPEG file is grayscale, you might want to do
  614.  * things differently than if it is color.  You can also delay setting
  615.  * quantize_colors and associated options until this point. 
  616.  *
  617.  * j_d_defaults initializes out_color_space to CS_RGB.  If you want grayscale
  618.  * output you should set out_color_space to CS_GRAYSCALE.  Note that you can
  619.  * force grayscale output from a color JPEG file (though not vice versa).
  620.  */
  621.  
  622. METHODDEF void
  623. d_ui_method_selection (decompress_info_ptr cinfo)
  624. {
  625.   /* if grayscale input, force grayscale output; */
  626.   /* else leave the output colorspace as set by main routine. */
  627.   if (cinfo->jpeg_color_space == CS_GRAYSCALE)
  628.     cinfo->out_color_space = CS_GRAYSCALE;
  629.  
  630.   /* select output routines */
  631.   cinfo->methods->output_init = output_init;
  632.   cinfo->methods->put_color_map = put_color_map;
  633.     switch(readtype) {
  634.         case JPEG_SCREEN:
  635.             cinfo->methods->put_pixel_rows = SCREENput_pixel_rows;
  636.             break;
  637.         case JPEG_PTR:
  638.             cinfo->methods->put_pixel_rows = PTRput_pixel_rows;
  639.             break;
  640.     }
  641.   cinfo->methods->output_term = output_term;
  642.     readxsize = cinfo->image_width;
  643.     readysize = cinfo->image_height;
  644.     jpegimgptr = (unsigned long*)malloc(sizeof(long)*(cinfo->image_width*cinfo->image_height)+1);
  645. }
  646.  
  647.  
  648. /*
  649.  * OK, here is the main function that actually causes everything to happen.
  650.  * We assume here that the JPEG filename is supplied by the caller of this
  651.  * routine, and that all decompression parameters can be default values.
  652.  * The routine returns 1 if successful, 0 if not.
  653.  */
  654.  
  655. GLOBAL int
  656. read_JPEG_file (filename,type)
  657. char *filename, type;
  658. {
  659.   /* These three structs contain JPEG parameters and working data.
  660.    * They must survive for the duration of parameter setup and one
  661.    * call to jpeg_decompress; typically, making them local data in the
  662.    * calling routine is the best strategy.
  663.    */
  664.   struct Decompress_info_struct cinfo;
  665.   struct Decompress_methods_struct dc_methods;
  666.   struct External_methods_struct e_methods;
  667.  
  668.   /* Select the input and output files.
  669.    * In this example we want to open the input file before doing anything else,
  670.    * so that the setjmp() error recovery below can assume the file is open.
  671.    * Note that cinfo.output_file is only used if your output handling routines
  672.    * use it; otherwise, you can just make it NULL.
  673.    * VERY IMPORTANT: use "b" option to fopen() if you are on a machine that
  674.    * requires it in order to read binary files.
  675.    */
  676.   if ((cinfo.input_file = fopen(filename, "rb")) == NULL) {
  677.     fprintf(stderr, "can't open %s\n", filename);
  678.     return 0;
  679.   }
  680.  
  681.     readtype = type;
  682.   cinfo.output_file = NULL;    /* if no actual output file involved */
  683.  
  684.   /* Initialize the system-dependent method pointers. */
  685.   cinfo.methods = &dc_methods;    /* links to method structs */
  686.   cinfo.emethods = &e_methods;
  687.   /* Here we supply our own error handler; compare to use of standard error
  688.    * handler in the previous write_JPEG_file example.
  689.    */
  690.   emethods = &e_methods;    /* save struct addr for possible access */
  691.   e_methods.error_exit = error_exit; /* supply error-exit routine */
  692.   e_methods.trace_message = trace_message; /* supply trace-message routine */
  693.   e_methods.trace_level = 0;    /* default = no tracing */
  694.   e_methods.num_warnings = 0;    /* no warnings emitted yet */
  695.   /* By default, the first corrupt-data warning will be displayed,
  696.    * but additional ones will appear only if trace level is at least 2.
  697.    */
  698.   e_methods.first_warning_level = 0;
  699.   e_methods.more_warning_level = 2;
  700.  
  701.   /* prepare setjmp context for possible exit from error_exit */
  702.   if (setjmp(setjmp_buffer)) {
  703.     /* If we get here, the JPEG code has signaled an error.
  704.      * Memory allocation has already been cleaned up (see free_all call in
  705.      * error_exit), but we need to close the input file before returning.
  706.      * You might also need to close an output file, etc.
  707.      */
  708.     fclose(cinfo.input_file);
  709.     return 0;
  710.   }
  711.  
  712.   /* Here we use the standard memory manager provided with the JPEG code.
  713.    * In some cases you might want to replace the memory manager, or at
  714.    * least the system-dependent part of it, with your own code.
  715.    */
  716.   jselmemmgr(&e_methods);    /* select std memory allocation routines */
  717.   /* If the decompressor requires full-image buffers (for two-pass color
  718.    * quantization or a noninterleaved JPEG file), it will create temporary
  719.    * files for anything that doesn't fit within the maximum-memory setting.
  720.    * You can change the default maximum-memory setting by changing
  721.    * e_methods.max_memory_to_use after jselmemmgr returns.
  722.    * On some systems you may also need to set up a signal handler to
  723.    * ensure that temporary files are deleted if the program is interrupted.
  724.    * (This is most important if you are on MS-DOS and use the jmemdos.c
  725.    * memory manager back end; it will try to grab extended memory for
  726.    * temp files, and that space will NOT be freed automatically.)
  727.    * See jcmain.c or jdmain.c for an example signal handler.
  728.    */
  729.  
  730.   /* Here, set up the pointer to your own routine for post-header-reading
  731.    * parameter selection.  You could also initialize the pointers to the
  732.    * output data handling routines here, if they are not dependent on the
  733.    * image type.
  734.    */
  735.   dc_methods.d_ui_method_selection = d_ui_method_selection;
  736.  
  737.   /* Set up default decompression parameters. */
  738.   j_d_defaults(&cinfo, TRUE);
  739.   /* TRUE indicates that an input buffer should be allocated.
  740.    * In unusual cases you may want to allocate the input buffer yourself;
  741.    * see jddeflts.c for commentary.
  742.    */
  743.  
  744.   /* At this point you can modify the default parameters set by j_d_defaults
  745.    * as needed; for example, you can request color quantization or force
  746.    * grayscale output.  See jdmain.c for examples of what you might change.
  747.    */
  748.     cinfo.quantize_colors = FALSE;
  749.  
  750.   /* Set up to read a JFIF or baseline-JPEG file. */
  751.   /* This is the only JPEG file format currently supported. */
  752.   jselrjfif(&cinfo);
  753.  
  754.   /* Here we go! */
  755.   jpeg_decompress(&cinfo);
  756.  
  757.   /* That's it, son.  Nothin' else to do, except close files. */
  758.   /* Here we assume only the input file need be closed. */
  759.   fclose(cinfo.input_file);
  760.  
  761.   /* You might want to test e_methods.num_warnings to see if bad data was
  762.    * detected.  In this example, we just blindly forge ahead.
  763.    */
  764.   return 1;            /* indicate success */
  765.  
  766.   /* Note: if you want to decompress more than one image, we recommend you
  767.    * repeat this whole routine.  You MUST repeat the j_d_defaults()/alter
  768.    * parameters/jpeg_decompress() sequence, as some data structures allocated
  769.    * in j_d_defaults are freed upon exit from jpeg_decompress.
  770.    */
  771. }
  772.  
  773. LoadJPEGImageScreen(char *name,int sx,int sy) {
  774.     readstartx = sx;
  775.     readstarty = sy;
  776.     crow = 0;
  777.     trow = 0;
  778.     read_JPEG_file(name,JPEG_SCREEN);
  779. }
  780.  
  781. unsigned long *LoadJPEGImagePtr(filename,xsize,ysize)
  782. char *filename;
  783. int *xsize, *ysize;
  784. {
  785.     crow = 0;
  786.     trow = 0;
  787.     read_JPEG_file(filename,JPEG_PTR);
  788.     *xsize = readxsize;
  789.     *ysize = readysize;
  790.     return(jpegimgptr);
  791. }
  792.  
  793. char *GetJPEGVersion() {
  794.     return(JVERSION);
  795. }
  796.